<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Crypt;
use Alert;
//引入验证码类
require './code/Code.class.php';
//类名 必须和文件名 一致
class AdminController extends Controller
{
	public function login()
	{
	    //判断提交的方式
          if($_SERVER['REQUEST_METHOD']== 'POST'){
              //接收数据
              $username=Input::get('username');
              $password=Input::get('password');
              $captcha=strtoupper(Input::get('captcha'));

              $code = new \Code();
              $sessionCode=$code->get();
              //获取的验证码和生成的验证码
              if($sessionCode !=$captcha ){
                  alert()->error("验证码有误", "提示信息")->autoclose(3500);
                  return back();
              }
              //验证用户信息
              $info=DB::table('user')->where('username','=',$username)->first();
                if($info){
                    if( Crypt::decrypt($info->password) == $password){
                        //保存session
                        session([
                            //把info 中的数据存放到sessio
                          'id'=> $info->id,
                          'username'=> $info->username,
                        ]);
                        return redirect('admin/index');
                    }else{
                        return back();
                    }
                }else{
                    return back();
                }
          }else{
              return view('admin/login');
          }
	}
 //验证码方法
    public function code()
    {
        $code = new \Code();
        $code->make();
    }

}
